package com.indexingsystem.system.service.impl;

import com.indexingsystem.boss.admin.mapper.IdxThemeTableGroupMapper;
import com.indexingsystem.boss.entity.IdxThemeTableGroup;
import com.indexingsystem.boss.entity.IdxThemeTableGroupWithBLOBs;
import com.indexingsystem.system.service.IdxThemeTableGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IdxThemeTableGroupServiceImpl implements IdxThemeTableGroupService {
    @Resource
    private IdxThemeTableGroupMapper idxThemeTableGroupMapper;

    @Override
    public int deleteByPrimaryKey(Integer themeGroupId) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.deleteByPrimaryKey(themeGroupId);
    }

    @Override
    public int insert(IdxThemeTableGroupWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.insert(record);
    }

    @Override
    public int insertSelective(IdxThemeTableGroupWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.insertSelective(record);
    }

    @Override
    public IdxThemeTableGroupWithBLOBs selectByPrimaryKey(Integer themeGroupId) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.selectByPrimaryKey(themeGroupId);
    }

    @Override
    public int updateByPrimaryKeySelective(IdxThemeTableGroupWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(IdxThemeTableGroupWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(IdxThemeTableGroup record) {
        // TODO Auto-generated method stub
        return idxThemeTableGroupMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<IdxThemeTableGroupWithBLOBs> selectThemeTableGroupByParentId(Integer parentId) {
        List<Integer> parentIdList = new ArrayList<Integer>();
        List<IdxThemeTableGroupWithBLOBs> parentList = new ArrayList<IdxThemeTableGroupWithBLOBs>();
        parentIdList.add(parentId);
        List<IdxThemeTableGroupWithBLOBs> list = selectAllParentIdByParentId(parentList, parentIdList);
        return list;
    }

    @Override
    public Integer updateThemeGroupArrchildIdByParentId(Integer themeGroupId, Integer parentId) {
        Integer value = null;
        StringBuffer sb = null;
        List<IdxThemeTableGroupWithBLOBs> parentList = new ArrayList<IdxThemeTableGroupWithBLOBs>();
        List<Integer> parentIdList = new ArrayList<Integer>();
        parentIdList.add( parentId );
        List<IdxThemeTableGroupWithBLOBs> list = selectAllParentIdByParentId(parentList,parentIdList);
        if (null != list && list.size()>0) {
            for (IdxThemeTableGroupWithBLOBs idxThemeTableGroupWithBLOBs : list) {
                sb = new StringBuffer();
                if (StringUtils.isNotBlank( idxThemeTableGroupWithBLOBs.getArrchildid() ) && !idxThemeTableGroupWithBLOBs.getArrchildid().equals( "-1" )) {
                    sb.append( idxThemeTableGroupWithBLOBs.getArrchildid() ).append( "," ).append( themeGroupId );
                }else{
                    sb.append( themeGroupId );
                }
                idxThemeTableGroupWithBLOBs.setChild( 1 );
                idxThemeTableGroupWithBLOBs.setArrchildid(  sb.toString() );
                value = idxThemeTableGroupMapper.updateByPrimaryKeySelective( idxThemeTableGroupWithBLOBs );
            }
        }
        return value;
    }

    @Override
    public List<IdxThemeTableGroupWithBLOBs> getThemeGroupById(Integer valueOf) {
        return idxThemeTableGroupMapper.getThemeGroupById( valueOf );
    }

    @Override
    public Integer deleteByIdBatch(List<String> list) {
        return idxThemeTableGroupMapper.deleteByIdBatch( list );
    }

    /**
     * 根据父级id查询所有父级id
     */
    public List<IdxThemeTableGroupWithBLOBs> selectAllParentIdByParentId(List<IdxThemeTableGroupWithBLOBs> parentList, List<Integer> parentIdList) {
        List<IdxThemeTableGroupWithBLOBs> IdxThemeTableGroupWithBLOBs = null;
        List<Integer> list = null;
        if (null != parentIdList && parentIdList.size() > 0) {
            IdxThemeTableGroupWithBLOBs = idxThemeTableGroupMapper.selectByParentId( parentIdList );
            if (null != IdxThemeTableGroupWithBLOBs && IdxThemeTableGroupWithBLOBs.size() > 0) {
                for (IdxThemeTableGroupWithBLOBs idxThemeTableGroupWithBLOBs : IdxThemeTableGroupWithBLOBs) {
                    list = new ArrayList<Integer>();
                    parentList.add( idxThemeTableGroupWithBLOBs );
                    if (null != idxThemeTableGroupWithBLOBs.getParentid() && idxThemeTableGroupWithBLOBs.getParentid() != -1) {
                        list.add( idxThemeTableGroupWithBLOBs.getParentid() );
                    } else {
                        list.add( null );
                    }
                }
            }
            selectAllParentIdByParentId( parentList, list );
        }
        return parentList;
    }



}
